import pandas as pd
import requests
from PIL import Image
import matplotlib.pyplot as plt
from io import BytesIO
from urllib.parse import urlencode

# 高德地图API Key
api_key = '2074e3830cf12c789ed409e1517eed6d'


# 定义评分函数
def calculate_score(方案数, 距离, max_distance, min_distance, max_schemes=3):
    # 行车方案数的评分（0到100）
    scheme_score = (方案数 / max_schemes) * 100

    # 最短距离的评分（0到100），距离越小，评分越高
    if min_distance != 0:  # 避免除以零
        distance_score = ((max_distance - 距离) / min_distance) * 100
    else:
        distance_score = 100

    # 综合评分，行车方案数和距离的权重相同
    score = (scheme_score + distance_score) / 2
    return score


# 处理数据，计算评分
def process_data(building, file_path, max_distance, min_distance, max_schemes=3):
    data = pd.read_excel(file_path)
    data['Score'] = data.apply(
        lambda row: calculate_score(row['行车方案数'], row['最短距离'], max_distance, min_distance, max_schemes),
        axis=1
    )
    return data


# 主函数
def main():
    # 文件路径
    file_paths = {
        "良友大厦": "E:\Program Files\Pythonproject\pythonProject1\良友大厦_行车方案数据.xlsx",
        "武银大厦": "E:\Program Files\Pythonproject\pythonProject1\武银大厦_行车方案数据.xlsx",
        "中诚大厦": "E:\Program Files\Pythonproject\pythonProject1\中诚大厦_行车方案数据.xlsx"
    }

    # 存储每个大厦的平均评分
    average_scores = {}

    # 读取每个大厦的数据，并计算最大和最小距离
    for building, file_path in file_paths.items():
        data = pd.read_excel(file_path)
        max_distance = data['最短距离'].max()
        min_distance = data['最短距离'].min()

        # 处理数据并计算评分
        processed_data = process_data(building, file_path, max_distance, min_distance)

        # 计算平均评分
        average_score = processed_data['Score'].mean()
        average_scores[building] = average_score

        print(f"{building} 的平均评分为: {average_score:.2f}")

    # 找出平均评分最高的大厦
    most_convenient_building = max(average_scores, key=average_scores.get)
    print(f"\n综合考虑行车方案数和最短距离，{most_convenient_building} 去所有目的地更方便。")


# 运行主函数
if __name__ == "__main__":
    main()